package controller

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gbuild"
	"github.com/gogf/gf/v2/os/gproc"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/util/gconv"
	v1 "websocket/api/v1"
	"websocket/internal/consts"
	"websocket/utility"
)

var (
	Common = cCommon{}
)

type cCommon struct{}

func (c *cCommon) StatusEnum(ctx context.Context, req *v1.CommonStatusEnumReq) (res *v1.CommonStatusEnumRes, err error) {
	res = new(v1.CommonStatusEnumRes)
	res.Http = consts.StatusHttp().Enum()
	res.Custom = consts.StatusCustom().Enum()
	return
}

func (c *cCommon) ServerDate(ctx context.Context, req *v1.CommonServerDateReq) (res *v1.CommonServerDateRes, err error) {
	res = new(v1.CommonServerDateRes)
	now := gtime.Now()
	res.Datetime = now.String()
	res.DatetimeMilli = now.Format("Y-m-d H:i:s.u")
	res.Timestamp = now.TimestampStr()
	res.TimestampMilli = now.TimestampMilliStr()
	res.Format = now.Format(req.Format)
	res.ISO8601 = now.ISO8601()
	res.RFC822 = now.RFC822()
	return
}

func (c *cCommon) Version(ctx context.Context, req *v1.CommonVersionReq) (res *v1.CommonVersionRes, err error) {
	res = new(v1.CommonVersionRes)
	res.Info = gbuild.Info()
	res.Datetime = gtime.Datetime()
	res.UUID = gconv.String(utility.IdGenerator.Get())
	res.Port = g.Server().GetListenedPort()
	res.Pid = gproc.Pid()
	res.StartDatetime = gtime.New(gproc.StartTime()).String()
	res.WorkId = g.Config().MustGet(ctx, "idGenerator.workId").Int()
	return
}
